package org.jeecg.modules.demo.dtwlwjc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.dtwlwjc.entity.ZwglShangqing;
import org.jeecg.modules.demo.dtwlwjc.service.IZwglShangqingService;
import org.jeecg.modules.demo.dtwlwjc.service.impl.TFSensorWeatherServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: 墒情数据
 * @Author: jeecg-boot
 * @Date: 2023-04-25
 * @Version: V1.0
 */
@Api(tags = "墒情数据")
@RestController
@RequestMapping("/dtwlwjc/zwglShangqing")
@Slf4j
public class ZwglShangqingController extends JeecgController<ZwglShangqing, IZwglShangqingService> {
    @Autowired
    private IZwglShangqingService zwglShangqingService;
    @Autowired
    private TFSensorWeatherServiceImpl TFSensorWeatherServiceImpl;
    @Autowired
    private IZwglShangqingService iZwglShangqingService;

    /**
     * 分页列表查询
     *
     * @param zwglShangqing
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    //@AutoLog(value = "墒情数据-分页列表查询")
    @ApiOperation(value = "墒情数据-分页列表查询", notes = "墒情数据-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<ZwglShangqing>> queryPageList(ZwglShangqing zwglShangqing,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                      HttpServletRequest req) {
        QueryWrapper<ZwglShangqing> queryWrapper = QueryGenerator.initQueryWrapper(zwglShangqing, req.getParameterMap());
        Page<ZwglShangqing> page = new Page<ZwglShangqing>(pageNo, pageSize);
        IPage<ZwglShangqing> pageList = zwglShangqingService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param zwglShangqing
     * @return
     */
    @AutoLog(value = "墒情数据-添加")
    @ApiOperation(value = "墒情数据-添加", notes = "墒情数据-添加")
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:add")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody ZwglShangqing zwglShangqing) {
        zwglShangqingService.save(zwglShangqing);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param zwglShangqing
     * @return
     */
    @AutoLog(value = "墒情数据-编辑")
    @ApiOperation(value = "墒情数据-编辑", notes = "墒情数据-编辑")
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:edit")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody ZwglShangqing zwglShangqing) {
        zwglShangqingService.updateById(zwglShangqing);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "墒情数据-通过id删除")
    @ApiOperation(value = "墒情数据-通过id删除", notes = "墒情数据-通过id删除")
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:delete")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id", required = true) String id) {
        zwglShangqingService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "墒情数据-批量删除")
    @ApiOperation(value = "墒情数据-批量删除", notes = "墒情数据-批量删除")
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:deleteBatch")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.zwglShangqingService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    //@AutoLog(value = "墒情数据-通过id查询")
    @ApiOperation(value = "墒情数据-通过id查询", notes = "墒情数据-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<ZwglShangqing> queryById(@RequestParam(name = "id", required = true) String id) {
        ZwglShangqing zwglShangqing = zwglShangqingService.getById(id);
        if (zwglShangqing == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(zwglShangqing);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param zwglShangqing
     */
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZwglShangqing zwglShangqing) {
        return super.exportXls(request, zwglShangqing, ZwglShangqing.class, "墒情数据");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequiresPermissions("dtwlwjc:zwgl_shangqing:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZwglShangqing.class);
    }

//    @GetMapping(value = "/test")
//    public Result getdksblist(@RequestParam(name = "deviceid", required = false) Integer deviceid) {
//        return TFSensorWeatherServiceImpl.transferData(deviceid);
//    }
    @GetMapping(value = "/test")
    public Result getdksblist(@RequestParam(name = "dkmc", required = false) String dkmc) {
        return TFSensorWeatherServiceImpl.transferData(dkmc);
    }


    @GetMapping(value = "/test1")
    public Result getdksblista(@RequestParam(name = "dkmc", required = false) String dkmc) {
        return TFSensorWeatherServiceImpl.transferDataa(dkmc);
    }

    @GetMapping(value = "/test2")
    public Result test2(@RequestParam(name = "deviceid", required = false) Integer deviceid,
                        @RequestParam(name = "page", required = false) Integer pageNo) {
        return TFSensorWeatherServiceImpl.transferDatb(pageNo, deviceid);
    }

    @GetMapping(value = "/test3")
    public Result test3(@RequestParam(name = "deviceid", required = false) Integer deviceid,
                        @RequestParam(name = "page", required = false) Integer pageNo) {
        return TFSensorWeatherServiceImpl.transferDatc(pageNo, deviceid);
    }

    @GetMapping(value = "/getsq")
    public Result getsq(@RequestParam(name = "deviceid", required = false) Integer deviceid) {
        return TFSensorWeatherServiceImpl.getsqsj(deviceid);
    }

    @GetMapping(value = "/getsssq")
    public Result getsssq(@RequestParam(name = "deviceid", required = false) Integer deviceid) {
        return TFSensorWeatherServiceImpl.getsqsssj(deviceid);
    }

    @GetMapping(value = "/getqixiangshijian")
    public Result getqixiangshijian(@RequestParam(name = "kaishishijian", required = false) String kaishishijian, @RequestParam(name = "jieshushijian", required = false) String jieshushijian, @RequestParam(name = "deviceid", required = false) Integer deviceid) {

        return TFSensorWeatherServiceImpl.getqixiangshijian(kaishishijian, jieshushijian, deviceid);
    }

    @GetMapping(value = "/qixiang")
    public Result qixiang(@RequestParam(name = "starttime", required = false) String starttime,
                          @RequestParam(name = "endtime", required = false) String endtime,
                          @RequestParam(name = "deviceid", required = false) Integer deviceid,
                          @RequestParam(name = "page", required = false) Integer pageNo
    ) {
        if (starttime=="" || endtime==""){
            return TFSensorWeatherServiceImpl.transferDatb(pageNo, deviceid);
        }else {
            return TFSensorWeatherServiceImpl.qixiangshijian(starttime, endtime, deviceid, pageNo);
        }

    }

    @GetMapping(value = "/shangqing")
    public Result shangqing(@RequestParam(name = "starttime", required = false) String starttime,
                            @RequestParam(name = "endtime", required = false) String endtime,
                            @RequestParam(name = "deviceid", required = false) Integer deviceid,
                            @RequestParam(name = "page", required = false) Integer pageNo
    ) {
        if (starttime=="" || endtime ==""){
            return TFSensorWeatherServiceImpl.transferDatc(pageNo, deviceid);
        }else {
            return TFSensorWeatherServiceImpl.shangqingshijian(starttime, endtime, deviceid, pageNo);
        }

    }

}
